package com.cssy.xxkj.logic.util;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.springframework.stereotype.Service;

import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;

@Service("oftenMothedUtil")
public class OftenMothedUtil {
	/**
	 * 增加搜索条件为本月的 field 为存放与本月匹配的字段
	 * @param scs
	 * @param field 需要增加排序的时间字段
	 * @return
	 */
	public SearchConditions addSearchConditionsByInMonth(SearchConditions scs,String field){
		if(scs==null){
			scs=new SearchConditions();
		}
		Collection<SearchCondition> condition=scs.getConditions();
		if(field==null){
			field="bizCreatorDate";
		}
		boolean isok=true;
		for(SearchCondition sc:condition){
			if(sc.getOperator().equals(SearchCondition.OP_GREATER)||sc.getOperator().equals(SearchCondition.OP_SMALLER)){
				isok=false;
			}
		}
		if(isok){
			scs.addCondition(new SearchCondition(field, BillStatusUtil.OP_INMONTH, new SimpleDateFormat("yyyyMM").format(new Date())));
		}
		return scs;
	}
	
	/**
	 * 在没有选择状态的情况下获取没有完成的单据
	 * @param scs
	 * @param field
	 * @return
	 */
	public SearchConditions getNoFinishConditions(SearchConditions scs,String field,String value)throws SyBaseException{
		if(scs==null){
			scs=new SearchConditions();
		}
		Collection<SearchCondition> condition=scs.getConditions();
		if(field==null){
			throw new SyBaseException("传入字段不能为空");
		}
		boolean isok=true;
		if(condition.size()>0){
			isok=false;
		}
		for(SearchCondition sc:condition){
			if(sc.getField().equals(field)){
				if(sc.getOperator().equals(SearchCondition.OP_NOTEQUAL)){
					isok=true;
				}
			}
		}
		if(isok){
			scs.addCondition(new SearchCondition(field, SearchCondition.OP_NOTEQUAL, value));
		}
		return scs;
	}
	/**
	 * 从page的搜索条件中的条件转换编码（中文的iso->utf-8）
	 * @param request
	 * @param fieldfield url 中存放参数的字段
	 * @return
	 */
	public SearchConditions getNewSearchConditionsPageToJava(HttpServletRequest request,String field){
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs= new SearchConditions();
		hander.getWheres(scs);
		try {
			if(field==null){
				field="filters";
			}
			hander.setFilters(new String(request.getParameter(field).getBytes(
					"ISO-8859-1"), "UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		scs.clearCondition();
		hander.getWheres(scs);
		return scs;
	}
	/**
	 * 获取没有任何参数限制的 SearchConditions
	 * romve 需要自己设置
	 * @param scs
	 * @return
	 */
	public SearchConditions getCleanCSearchCondition(SearchConditions scs){
		scs.clearCondition();
		scs.setLimitClause(null);
		scs.setLimitPage(false);
		return scs;
	}
	
	public String getStringCellValue(HSSFCell cell) {
		if(cell==null||"".equals(cell)){
			return "";
		}
		String strCell = "";
		switch (cell.getCellType()) {
			case HSSFCell.CELL_TYPE_STRING:
				strCell = cell.getStringCellValue();
				break;
			case HSSFCell.CELL_TYPE_NUMERIC:
				strCell = String.valueOf(cell.getNumericCellValue());
				break;
			case HSSFCell.CELL_TYPE_BOOLEAN:
				strCell = String.valueOf(cell.getBooleanCellValue());
				break;
			case HSSFCell.CELL_TYPE_BLANK:
				strCell = "";
				break;
			default:
				strCell = "";
				break;
		}
		return strCell;
	}
}
